﻿using Admin.CommonLib.Models.Permission;
using Newtonsoft.Json;
using System.Net.Http.Json;
using Admin.CommonLib.Constants;
using Swimj.CommonLib.Base;
using Swimj.Core.Extensions;

namespace Admin.RazorLib.Services
{
    public class PermissionService : IPermissionService
    {
        private readonly IHttpClientFactory _httpClientFactory;

        public PermissionService(IHttpClientFactory httpClientFactory)
        {
            _httpClientFactory = httpClientFactory;
        }

        public async Task<Result<PermissionInfoResponseDto>> CreatePermission(PermissionEditRequestDto requestDto)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("adminApi");
                var response = await httpClient.PostAsJsonAsync($"/{ApiRoutes.Permission.CreatePermission}", requestDto);
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<PermissionInfoResponseDto>>(resultJson);
                    }
                }

                return new Result<PermissionInfoResponseDto>();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<Result<IList<PermissionTreeResponseDto>>> QueryPermissionsByTree(int treeType)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("adminApi");
                var response = await httpClient.GetAsync($"/{ApiRoutes.Permission.TreePermission}?treeType={treeType}");
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<IList<PermissionTreeResponseDto>>>(resultJson);
                    }
                }

                return new Result<IList<PermissionTreeResponseDto>>();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<PaginatedResult<PermissionInfoResponseDto>> QueryPermissions(
            PermissionSearchRequestDto searchRequestDto)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("adminApi");
                var response = await httpClient.GetAsync(
                    searchRequestDto.ToQueryString(ApiRoutes.Permission.QueryPermissions));
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<PaginatedResult<PermissionInfoResponseDto>>(resultJson);
                    }
                }

                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<Result<PermissionInfoResponseDto>> UpdatePermission(Guid key, PermissionEditRequestDto model)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("adminApi");
                var response = await httpClient.PutAsJsonAsync($"/{ApiRoutes.Permission.UpdatePermission.Replace("{id}", key.ToString())}", model);
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<PermissionInfoResponseDto>>(resultJson);
                    }
                }

                return new Result<PermissionInfoResponseDto>();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<Result<PermissionInfoResponseDto>> GetPermission(Guid key)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("adminApi");
                var response = await httpClient.GetAsync($"/{ApiRoutes.Permission.GetPermission.Replace("{id}", key.ToString())}");
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<PermissionInfoResponseDto>>(resultJson);
                    }
                }

                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<Result<IList<PermissionTreeResponseDto>>> QueryPermissionsByCurrentUser(int treeType)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("adminApi");
                var response = await httpClient.GetAsync($"/{ApiRoutes.Permission.CurrentUserPermission}?treeType={treeType}");
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<IList<PermissionTreeResponseDto>>>(resultJson);
                    }
                }

                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<HttpResponseMessage> ExportPermissions()
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("adminApi");
                var response = await httpClient.GetAsync($"/{ApiRoutes.Permission.ExportPermission}");
                return response;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<HttpResponseMessage> ExportTemplate()
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("adminApi");
                var response = await httpClient.GetAsync($"/{ApiRoutes.Permission.ExportTemplatePermission}");
                return response;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public Task<Result<IList<PermissionInfoResponseDto>>> ImportPermissions(int importType, string uploadFileName, Stream openReadStream)
        {
            throw new NotImplementedException();
        }
    }
}